/*
 * @Description: 
 * @Author: BMIU
 * @Date: 2022-01-15 10:27:19
 * @LastEditors: BMIU
 * @LastEditTime: 2022-01-15 16:35:35
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import SpringShader from "./SpringShader";

const { ccclass, property } = cc._decorator;



@ccclass
export default class SpringScene extends cc.Component {
    @property(cc.Camera)
    mainCamera: cc.Camera = null;

    @property(cc.Camera)
    renderCamera: cc.Camera = null;

    @property(cc.Sprite)
    sp: cc.Sprite = null;

    @property(SpringShader)
    springShader: SpringShader = null;

    protected onEnable(): void {
        // cc.systemEvent.on(cc.SystemEvent.EventType.)
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
    }

    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
    }

    protected onLoad(): void {
        //开启碰撞
        cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;

        //将渲染纹理设置到场景摄像机上
        let renderTexture = this.createTexture();
        this.mainCamera.targetTexture = renderTexture;
        let sf = new cc.SpriteFrame();
        sf.setTexture(renderTexture);
        sf.setFlipY(true);
        this.sp.spriteFrame = sf;
    }

    touchStart(event) {
        this.springShader.springAt(event.getLocation());
        // this.springShader.springAt(this.sp.node.convertToWorldSpaceAR(cc.Vec2.ZERO_R));
    }

    createTexture(): cc.RenderTexture {
        let texture = new cc.RenderTexture();
        texture.initWithSize(cc.winSize.width, cc.winSize.height, cc.RenderTexture.DepthStencilFormat.RB_FMT_D24S8);
        return texture;
    }
}
// const { ccclass, property } = cc._decorator;

// @ccclass
// export default class NewClass extends cc.Component {
//     @property(cc.Sprite)
//     sp: cc.Sprite = null;

//     private _mat: cc.Material = null;

//     @property
//     time: number = 5;

//     @property
//     a: number = 150;

//     @property
//     w: number = 0.01;

//     @property(cc.Camera)
//     camera: cc.Camera = null;

//     private _enableSpring: boolean = false;
//     private _startTime: number = 0;

//     onEnable() {
//         this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//     }

//     onDisable() {
//         this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//     }

//     onTouchStart(event) {
//         this._mat.setProperty("centerPos", event.getLocation());
//         this.startSpring();
//     }

//     start() {
//         this.camera.targetTexture = this.sp.spriteFrame.getTexture() as cc.RenderTexture;
        
//         this._mat = this.sp.getMaterial(0);

//         this._mat.setProperty("centerPos", this.sp.node.convertToWorldSpaceAR(cc.Vec2.ZERO_R));
//         this._mat.setProperty("ratio", cc.v2(1 / this.sp.node.width, 1 / this.sp.node.height));
//         this._mat.setProperty("range", 400);
//     }


//     update(dt) {
//         if (this._enableSpring) {
//             let t = (cc.director.getTotalTime() * 0.001 - this._startTime) / this.time;
//             if (t <= 1) {
//                 let offset = this.a * ((Math.sin(this.w * t) + 1) * 0.5);
//                 offset *= Math.exp(-t * 5);
//                 cc.log(offset);
//                 this._mat.setProperty("offset", offset);
//             } else {
//                 this._enableSpring = false;
//                 this._mat.setProperty("offset", 0);
//             }
//         }
//     }

//     startSpring() {
//         this._enableSpring = true;
//         this._startTime = cc.director.getTotalTime() * 0.001;
//     }
// }
